package com.cn.xiaonuo.slbfinances.slcoupon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.DateTimeUtils;
import com.cn.xiaonuo.common.utils.ExportExcelUtils;
import com.cn.xiaonuo.common.vo.ExcelData;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slcoupon.mapper.CmExReleaseMapper;
import com.cn.xiaonuo.slbfinances.slcoupon.mapper.GzCouponMapper;
import com.cn.xiaonuo.slbfinances.slcoupon.service.IGzCouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GzCouponServiceImpl extends ServiceImpl<GzCouponMapper, String> implements IGzCouponService {
    @Resource
    private GzCouponMapper gzCouponMapper;
    @Resource
    private CmExReleaseMapper cmExReleaseMapper;
    @Autowired
    private Environment env;

    //查询用户国甄购物券
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage qryUserGzBalance(String checkState, String userId, String phone, Integer pageNum, Integer pageSize) {
        BusinessMessage message = new BusinessMessage();
        try {
            //1.设置分页参数
            if (null == pageNum || pageNum < 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize < 1) {
                pageSize = 10;
            }
            //2.分页
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> maps = gzCouponMapper.qryUserGzBalance(checkState, userId, phone);
            PageInfo<Map<String, Object>> data = new PageInfo<>(maps);
            //3.返回数据
            message.setData(data);
            message.setMessage("操作成功");
            message.setSuccess(true);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    //搜了宝购物券收支记录--搜了宝购物券收支记录
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage selectGwjInfoOut(String userId, Long beginTime, Long endTime, Integer pageNum, Integer pageSize) {
        BusinessMessage message = new BusinessMessage();
        try {
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }
            String beginTimes = null;
            String endTimes = null;
            if (null != beginTime && null != endTime) {
                Map<String, String> map = DateTimeUtils.conversionTime(beginTime, endTime);
                beginTimes = map.get("begin");
                endTimes = map.get("end");
            }
            List<Map<String, Object>> resunum = cmExReleaseMapper.selectGwjInfoOut(userId, beginTimes, endTimes);
            PageHelper.startPage(pageNum, pageSize);  // 设置分页参数
            List<Map<String, Object>> resu = cmExReleaseMapper.selectGwjInfoOut(userId, beginTimes, endTimes);
            PageInfo<Map<String, Object>> sult = new PageInfo<>(resu);
            Map<String, Object> map = new HashMap<>(16);
            map.put("lastResult", sult);
            map.put("nums", null == resunum ? 0 : resunum.size());
            message.setData(map);
            message.setCode("200");
            message.setSuccess(true);
            message.setMessage("操作成功！");
        } catch (Exception e) {
            message.setSuccess(false);
            message.setMessage("操作失敗！");
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage selectGwjInfoIn(String userId, Long beginTime, Long endTime, Integer pageNum, Integer pageSize) {
        BusinessMessage message = new BusinessMessage();
        try {
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }
            String beginTimes = null;
            String endTimes = null;
            if (null != beginTime && null != endTime) {
                Map<String, String> map = DateTimeUtils.conversionTime(beginTime, endTime);
                beginTimes = map.get("begin");
                endTimes = map.get("end");
            }
            List<Map<String, Object>> resunum = cmExReleaseMapper.selectGwjInfoIn(userId, beginTimes, endTimes);
            PageHelper.startPage(pageNum, pageSize);  // 设置分页参数
            List<Map<String, Object>> resu = cmExReleaseMapper.selectGwjInfoIn(userId, beginTimes, endTimes);
            PageInfo<Map<String, Object>> sult = new PageInfo<>(resu);
            Map<String, Object> map = new HashMap<>(16);
            map.put("lastResult", sult);
            map.put("nums", null == resunum ? 0 : resunum.size());
            message.setData(map);
            message.setCode("200");
            message.setSuccess(true);
            message.setMessage("操作成功！");
        } catch (Exception e) {
            message.setSuccess(false);
            message.setMessage("操作失敗！");
            e.printStackTrace();
        }
        return message;
    }

}
